<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
error_reporting(E_ALL);
header('Content-Type: text/html; charset=UTF-8');
$HOME = explode("install/", $_SERVER['PHP_SELF']);
$HOME = reset($HOME);
$HOME = "http://" . $_SERVER['HTTP_HOST'] . $HOME;

define('ROOT', dirname(dirname(__file__)));
define('LINK_ROOT', $HOME);

@session_start();

if (isset($_GET['step'])) {
	$step = (int)$_GET['step'];
} else {
	$step = 1;
}
function form_start($method = 'post', $action = '', $datatransfer = '') {
	$e = '';
	if (!$action) {
		$LR = parse_url(LINK_ROOT);

		$action = $LR['scheme'].'://'.$LR['host'] . $_SERVER['REQUEST_URI'];
	}
	if ($datatransfer) {
		$e = ' enctype="multipart/form-data"';
	}

	$action = str_replace('&amp;', '&', $action);
	$action = str_replace('&', '&amp;', $action);
	return '<form method="' . $method . '"' . $e . ' action="' . $action . '" >';
}
function format($str) {
	$farg = func_get_args();
	$fnar = func_num_args();

	for ($i = 1; $i < $fnar; $i++) {
		$str = str_replace('{%' . $i . '%}', $farg[$i], $str);
	}

	return $str;
}
function read_directory($path, $onlydirs = false, $onlyfiles = false, $ext = null) {
	$ret = array();
	if (!$path) {
		return array();
	}
	

	$path = rtrim($path,'\\/') . DIRECTORY_SEPARATOR;

	$dh = opendir($path);
	if (!$dh) {
		return false;
	}
	while (!is_bool($ddt = readdir($dh))) {
		if ($ddt == '.' or $ddt == '..') {
			continue;
		}
		if ($onlydirs) {
			if (is_dir($path . $ddt)) $ret[] = $ddt;
		} elseif ($onlyfiles) {
			if (is_file($path . $ddt)) {
				if ($ext) {
					$mns = strlen($ext) + 1;
					if (substr($path . $ddt, -$mns) == '.' . $ext) {
						$ret[] = $ddt;
					}
				} else {
					$ret[] = $ddt;
				}
			}
		} else {
			$ret[] = $ddt;
		}
	}
	closedir($dh);
	return $ret;
}

include (ROOT . '/engine/classes/html/class.php');
include (ROOT . '/install/class.lang.php');

if (isset($_SESSION['lang'])) {
	Lang::init($_SESSION['lang']);
}

define('INSTALL_AURUS', true);
define('HEADER', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>Installation (step ' . $step . ')</title></head><body><div align="center">');
define('FOOTER', '</div></body></html>');

if (is_file(ROOT . '/engine/configs/config.db.php') or is_file(ROOT . '/engine/configs/config.main.php')) {
	die(HEADER . __('Скрипт вже встановлено, щоб повторити встановлення видаліть файл <strong>engine/configs/config.db.php</strong> та <strong>engine/configs/config.main.php</strong>') . FOOTER);
}

include ('./step/' . $step . '.php');
